/**
 * ReportGear(2011)
 */
package com.reportgear.designer.editor.object;

import java.awt.FlowLayout;

import javax.swing.JCheckBox;

/**
 * 布尔型编辑器
 * 
 * @version 1.0 2011-4-26
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class BooleanEditor extends Editor {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 7466376547343491602L;
	private JCheckBox booleanCheckBox;

	public BooleanEditor() {
		this(null);
	}

	public BooleanEditor(String name) {
		this(null, name);
	}

	public BooleanEditor(Object value) {
		this(value, "");
	}

	public BooleanEditor(Object value, String name) {
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		this.booleanCheckBox = new JCheckBox("true");
		this.add(this.booleanCheckBox);

		this.setValue(value);
		this.setName(name);
	}

	public Object getValue() {
		return new Boolean(this.booleanCheckBox.isSelected());
	}

	public void setValue(Object value) {
		if ((value == null) || (!(value instanceof Boolean))) {
			value = new Boolean(true);
		}

		this.booleanCheckBox.setSelected(((Boolean) value).booleanValue());
	}

	public void setEnabled(boolean enabled) {
		this.booleanCheckBox.setEnabled(enabled);
	}

	public void requestFocus() {
		this.booleanCheckBox.requestFocus();
	}

	protected void fireEditingStopped() {
		setValue(new Boolean(this.booleanCheckBox.isSelected()));

		super.fireEditingStopped();
	}

	public boolean accept(Object value) {
		return (value instanceof Boolean);
	}
}